package com.wangxi.boot10pro.controller;

import com.wangxi.boot10pro.entity.User;
import com.wangxi.boot10pro.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;

    @RequestMapping("/login")
    public int login(@RequestBody User user) {
        System.out.println("user = " + user);
        User u = mapper.selectUser(user.getUsername());
        System.out.println("u " + u.toString());
        if (u != null) {
            if (u.getPassword().equals(user.getPassword())) {
                return 1;
            }
            return 3;
        }
        return 2;
    }

    @RequestMapping("/reg")
    public int reg(@RequestBody User user){
        User u = mapper.selectUser(user.getUsername());
        if(u==null){
            mapper.insertUser(user);
            return 1;
        }
        return 2;
    }

}
